package com.example.socket.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname HttpRequestParser
 * @Description TODO
 * @Date 2023/4/27 11:49
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class HttpRequestParser {

    public static void main(String[] args) {
        String input = "GET /index.html HTTP/1.1\r\n" +
                "Host: www.example.com\r\n" +
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36\r\n" +
                "Accept-Language: en-US,en;q=0.9\r\n" +
                "\r\n" +
                "Hello, world!";

        String patternString = "^(?<method>\\w+)\\s+(?<path>[^\\s]+)\\s+HTTP/(?<version>\\d\\.\\d)\r\n" +
                "(?<headers>(.+?\r\n)+?)\r\n";


        Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            String method = matcher.group("method");
            String path = matcher.group("path");
            String version = matcher.group("version");

            Map<String, String> headers = new HashMap<>();
            String headersString = matcher.group("headers");
            String[] headerLines = headersString.split("\r\n");
            for (String headerLine : headerLines) {
                int colonIndex = headerLine.indexOf(':');
                if (colonIndex > 0) {
                    String key = headerLine.substring(0, colonIndex).trim();
                    String value = headerLine.substring(colonIndex + 1).trim();
                    headers.put(key, value);
                }
            }

            System.out.println("Method: " + method);
            System.out.println("Path: " + path);
            System.out.println("Version: " + version);
            System.out.println("Headers: " + headers);
        } else {
            System.out.println("No HTTP request found");
        }
    }

    public static Map<String,String> getHeaders(String content){
        String patternString = "^(?<method>\\w+)\\s+(?<path>[^\\s]+)\\s+HTTP/(?<version>\\d\\.\\d)\r\n" +
                "(?<headers>(.+?\r\n)+?)\r\n";

        Map<String, String> headers = new HashMap<>();
        Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(content);

        if (matcher.find()) {
            String method = matcher.group("method");
            String path = matcher.group("path");
            String version = matcher.group("version");


            String headersString = matcher.group("headers");
            String[] headerLines = headersString.split("\r\n");
            for (String headerLine : headerLines) {
                int colonIndex = headerLine.indexOf(':');
                if (colonIndex > 0) {
                    String key = headerLine.substring(0, colonIndex).trim();
                    String value = headerLine.substring(colonIndex + 1).trim();
                    headers.put(key, value);
                }
            }
            headers.put("Method",method);
            headers.put("Path",path);
            headers.put("Version",version);
            System.out.println("Method: " + method);
            System.out.println("Path: " + path);
            System.out.println("Version: " + version);
            System.out.println("Headers: " + headers);
        } else {
            System.out.println("No HTTP request found");
        }
        return headers;
    }
}
